/*
 * This project provides a number of implementations for indexing and evaluating
 * <i>Continuous Top-k Queries over Text Streams</i>
 * and has been developped as part of the work described in the article:
 * 
 * Vouzoukidou, N., Amann, B., & Christophides, V. (2012, October). 
 * Processing continuous text queries featuring non-homogeneous scoring functions.
 * In Proceedings of the 21st ACM international conference on Information and 
 * knowledge management (pp. 1065-1074). ACM.
 * http://dl.acm.org/citation.cfm?id=2398404
 * 
 * This software is distributed under the GNU General Public License and as an
 * additional condition of usage, please cite the aforementioned article.
 * 
 * Copyright (C) 2012 ICS/FORTH, Greece and 
 *               2012 LIP6, France
 * 
 * Author: Vouzoukidou Nelly, email: nelly.vouzoukidou (at) gmail (dot) com
 * 
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
 */
package examples;

import gr.forth.ics.continuoustopk.Item;
import gr.forth.ics.continuoustopk.datasets.DatasetConstants;
import gr.forth.ics.continuoustopk.datasets.ItemsIterator;
import gr.forth.ics.continuoustopk.datasets.QueriesIterator;
import gr.forth.ics.continuoustopk.index.QueryIndex;
import gr.forth.ics.continuoustopk.index.QueryIndexes;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.List;

/**
 * This is only a sample experiment counting the time to perform the matching.
 *
 * @author Vouzoukidou Nelly, email: nelly.vouzoukidou (at) gmail (dot) com
 */
public class Experiment {
    public static void main(String[] args) throws FileNotFoundException {
        
        // All possible QueryIndex instances are created.
        QueryIndex[] indexes = {
            QueryIndexes.sortedQueries(20),
            QueryIndexes.sortedBuckets(20, 4000),
            QueryIndexes.rectangularGrid(20, 1800),
            QueryIndexes.densityBuckets(3, 16),
            QueryIndexes.constantAngleOrdering(),
            QueryIndexes.colFilter()};
        
        // For this sample experiment we will only perform the test on the 
        // "sortedBuckets(20, 4000)" implementation:
        QueryIndex index = indexes[1];
        
        // Loading a sample of the queries found in file "queriesTfIdf.txt"
        // This way of loading queries is only a conviniency example. Otherwise
        // queries can be loaded from a file with any format and Query objects can
        // be created in other ways (see QueryItemIndexCreation.java for an example 
        // of query creation.
        QueriesIterator qit = QueriesIterator.mixedQueries();
        System.out.println("Adding queries...");
        int qCount = 0;
        while (qit.hasNext()) {
            index.add(qit.next());
            qCount++;
        }
        System.out.println("All " + qCount + " queries are added.");
        
//        index.addListener(QueryUpdatedListener.counter);
        // Here too, this is an example way of loading the items for a file (here
        // the file is "itemsTfIdf15000.txt". See QueryItemIndexCreation.java for 
        // an example of alternative item creation.
        ItemsIterator itemsIter = ItemsIterator.pressItems();
        List<Item> items = new ArrayList<Item>();
        for (int i = 0; i < DatasetConstants.itemCount; i++) {
            items.add(itemsIter.next());
        }
        
        
        // Some warmup of matching items is *necessary*. Otherwise all queries 
        // will initially have 0 as minimum score and the test will not show the
        // actual performance of the matching operation.
        for (int i = 0; i < DatasetConstants.warmUp; i++) {
            Item item = items.get(i);
            index.match(item);
        }
        
        // The actual experiment: counting the total time to match "itemCount - warmup"
        // items over the queries workload. Here the only result of the execution 
        // will be the time for the execution. For another example producing 
        // much more information as output see "MathcingInfo.java".
        long time = -System.currentTimeMillis();
        for (int i = DatasetConstants.warmUp; i < DatasetConstants.itemCount; i++) {
            Item item = items.get(i);
            index.match(item);
        }
        time += System.currentTimeMillis();
        System.out.println("time " + time);
    }
}
